# -*- coding: utf-8 -*-
# vim:et:sw=4:sts=4:ai:nosi
# Copyright (c) 2012 Flibusta Quotes Team
#
# This file is part of Flibusta Quotes.
#
# Flibusta Quotes is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from google.appengine.ext import db
from base import BaseModel

from config import COMMENTS_PAGESIZE


class Comment(BaseModel):
    content = db.TextProperty(indexed=False)
    submitted = db.DateTimeProperty(auto_now_add=True)
    submitter = db.UserProperty(indexed=False)

    @classmethod
    def fetch_last_comments(cls, n=COMMENTS_PAGESIZE):
        """Fetch last comments"""
        # Construct query for the last comments
        query = cls.all()
        query.order('-submitted')

        # Fetch up to n last comments
        comments = query.fetch(n)

        return comments

    @classmethod
    def fetch_comments_on_quote(cls, key, before=None, n=COMMENTS_PAGESIZE+1):
        """Fetch comments on a quote before a certain time"""
        # Construct query for comments
        query = cls.all()
        query.ancestor(key)
        if before:
            query.filter('submitted <=', before)
        query.order('-submitted')

        # Fetch up to n comments from the datastore
        comments = query.fetch(n)

        return comments

